package com.easyclearance.baseutil;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.easyclearance.entity.PassportInfo;

import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.poi.excel.ExcelReader;


/*******************************************************************************
 * FileControl 是一个用于处理文件的功能包，主要是复制文件，重命名文件、删除文件、存储文件、删除文件夹等
 *
 * @author Sunshine
 * @ClassName: FileControl
 * @Description: TODO
 * @author travbao@163.com
 * @date 06, 13, 2019 9:28:28 AM
 */
@Repository
public class FileControl {

    // 文件复制
    public static boolean copyFile(String source, String copy) throws Exception {

        File source_file = new File(source);
        File copy_file = new File(copy);

        // BufferedStream缓冲字节流

        if (!source_file.exists()) {
            throw new IOException("文件复制失败：源文件（" + source_file + "） 不存在");
        }
        if (copy_file.isDirectory()) {
            throw new IOException("文件复制失败：复制路径（" + copy_file + "） 错误");
        }
        File parent = copy_file.getParentFile();
        // 创建复制路径
        if (!parent.exists()) {
            parent.mkdirs();
        }
        // 创建复制文件
        if (!copy_file.exists()) {
            copy_file.createNewFile();
        }

        FileInputStream fis = new FileInputStream(source_file);
        FileOutputStream fos = new FileOutputStream(copy_file);

        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        byte[] KB = new byte[1024];
        int index;
        while ((index = bis.read(KB)) != -1) {
            bos.write(KB, 0, index);
        }

        bos.close();
        bis.close();
        fos.close();
        fis.close();

        if (!copy_file.exists()) {
            return false;
        } else if (source_file.length() != copy_file.length()) {
            return false;
        } else {
            return true;
        }

    }

    // 文件重命名
    public static boolean renameFile(String url, String new_name) throws Exception {
        String old_url = url;
        old_url = old_url.replace("\\", "/");
        File old_file = new File(old_url);
        if (!old_file.exists()) {
            throw new IOException("文件重命名失败，文件（"+old_file+"）不存在");
        }
        System.out.println(old_file.exists());

        String old_name = old_file.getName();
        // 获得父路径
        String parent = old_file.getParent();
        // 重命名
        String new_url = parent + "/" + new_name;
        File new_file = new File(new_url);
        old_file.renameTo(new_file);

        System.out.println("原文件：" + old_file.getName());
        System.out.println("新文件：" + new_file.getName());
        new_name = new_file.getName();
        old_name = old_file.getName();
        if (new_name.equals(old_name)) {
            return false;
        } else {
            return true;
        }

    }

    // 文件删除
    public static boolean deleteFile(String url) throws Exception {
        url = url.replace("\\", "/");
        File file = new File(url);

        if (file.isFile()) {
            if (file.exists()) {
                file.delete();
            }
        }else{
            throw new IOException("文件删除失败：（"+file+"）错误");
        }
        if (file.exists()) {
            return false;
        } else {
            return true;
        }
    }

    // 创建文件夹
    public static boolean createPath(String url) throws Exception {
        url = url.replace("\\", "/");
        File folder = new File(url);
        if(!folder.isDirectory()){
            throw new IOException("创建文件夹失败：（"+folder+"）不是文件夹路径");
        }

        if (!folder.isFile()) {
            if (!folder.exists()) {
                folder.mkdirs();
            }
        }
        // 检测是否创建成功
        if (folder.isDirectory() && folder.exists()) {
            return true;
        } else {
            return false;
        }

    }


    /**
     * @ 存储文件
     * @ param inputStream
     * @ param path
     * @throws IOException
     */
    @SuppressWarnings("resource")
    public static void saveFile(InputStream inputStream,String path) throws IOException {
        // 通过url获取文件

        // 通过inputStream获取文件
//	        InputStream inputStream = new FileInputStream(f);
        // 定义一个文件名字进行接收获取文件
        FileOutputStream fileOut = new FileOutputStream(new File(path));
        byte[] buf = new byte[1024 * 8];
        while (true) {
            int read = 0;
            if (inputStream != null) {
                read = inputStream.read(buf);
            }
            if (read == -1) {
                break;
            }
            fileOut.write(buf, 0, read);
        }
        // 查看文件获取是否成功
        if (fileOut.getFD().valid() == true) {
            System.out.println("获取文件保存成功");
        } else {
            System.out.println("获取文件失败");
        }
        fileOut.flush();
    }

    /**
     * @ 删除文件夹
     * @ param path
     * @ return
     */
    public static boolean deleteDir(String path){
        File file = new File(path);
        if(!file.exists()){//判断是否待删除目录是否存在
            System.err.println("The dir are not exists!");
            return false;
        }

        String[] content = file.list();//取得当前目录下所有文件和文件夹
        for(String name : content){
            File temp = new File(path, name);
            if(temp.isDirectory()){//判断是否是目录
                deleteDir(temp.getAbsolutePath());//递归调用，删除目录里的内容
                temp.delete();//删除空目录
            }else{
                if(!temp.delete()){//直接删除文件
                    System.err.println("Failed to delete " + name);
                }
            }
        }
        //删除空目录
        boolean success = (new File(path)).delete();
        if (success) {
            System.out.println("Successfully deleted empty directory: " + path);
        } else {
            System.out.println("Failed to delete empty directory: " + path);
        }
        return true;
    }

    /**
     * @ 删除文件
     * @ param fileName
     * @ return
     */
    public static boolean deleteFiles(String fileName){
        File file = new File(fileName);
        if(file.isFile() && file.exists()){
            Boolean succeedDelete = file.delete();
            if(succeedDelete){
                System.out.println("删除单个文件"+fileName+"成功！");
                return true;
            }
            else{
                System.out.println("删除单个文件"+fileName+"失败！");
                return true;
            }
        }else{
            System.out.println("删除单个文件"+fileName+"失败！");
            return false;
        }
    }

    /**
     * @ 阅读文件
     * @ param in
     */
    public static String ReadFile(InputStream in){
        byte b[] = new byte[1024];
        int len = 0;
        int temp=0;          //所有读取的内容都使用temp接收
        try {
            System.out.println(in.read());
            while((temp=in.read())!=-1){    //当没有读取完时，继续读取
                b[len]=(byte)temp;
                len++;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new String(b,0,len-1);
    }


    /**
     * @throws Exception
     * @ 删除目录（文件夹）以及目录下的文件
     * @ param   sPath 被删除目录的文件路径
     * @ return  目录删除成功返回true，否则返回false
     */
    public static boolean deleteDirectory(String sPath) throws Exception {
        //如果sPath不以文件分隔符结尾，自动添加文件分隔符
        if (!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }
        File dirFile = new File(sPath);
        //如果dir对应的文件不存在，或者不是一个目录，则退出
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        boolean flag = true;
        //删除文件夹下的所有文件(包括子目录)
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            //删除子文件
            if (files[i].isFile()) {
                flag = deleteFile(files[i].getAbsolutePath());
                if (!flag) break;
            } //删除子目录
            else {
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag) break;
            }
        }
        if (!flag) return false;
        //删除当前目录
        if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * * 删除空目录
     * @ param dir 将要删除的目录路径
     */
    public static void doDeleteEmptyDir(String dir) {
        boolean success = (new File(dir)).delete();
        if (success) {
            System.out.println("Successfully deleted empty directory: " + dir);
        } else {
            System.out.println("Failed to delete empty directory: " + dir);
        }
    }

    /**
     * 读取文件
     * @param filePath
     */
    @SuppressWarnings("resource")
	public String readFileString(String filePath){
        try {
            InputStream fileInputStream = new FileInputStream(new File(filePath));
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            StringBuffer stringBuffer = new StringBuffer();
            String lineString = "";
            while((lineString = bufferedReader.readLine())!=null){
                stringBuffer.append(lineString);
            }
            return stringBuffer.toString();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    /**
     * @读取后缀
     * @param filePath
     */
    @SuppressWarnings("resource")
	public static String getFileExtName(File file) throws IOException{
        String name = file.getName();
		String[] arr = name.split("。");
		if (arr.length>1) {
			return "."+arr[arr.length-1];
		}else {
			return null;
		}
    }
    
    public static PassportInfo mapconverPassportInfo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		PassportInfo passportInfo = new PassportInfo();
		try {
			//中文姓名
			String guName = "";
			if (map.containsKey("姓名") || map.containsKey("中文名") || map.containsKey("游客姓名") || map.containsKey("中文姓名") || map.containsKey("中文姓名CHINESENAME")) {
				if (map.containsKey("姓名")) {
					guName = map.get("姓名").toString();
				}else if (map.containsKey("中文名")) {
					guName = map.get("中文名").toString();
				}else if (map.containsKey("游客姓名")) {
					guName = map.get("游客姓名").toString();
				}else if (map.containsKey("中文姓名")) {
					guName = map.get("中文姓名").toString();
				}else if (map.containsKey("中文姓名CHINESENAME")) {
					guName = map.get("中文姓名CHINESENAME").toString();
				}
			}
			passportInfo.setGuName(guName);
			//英文姓名
			String guFirstName = "";
			String guFamilyName = "";
			if ((map.containsKey("姓 拼音") && map.containsKey("名 拼音")) || (map.containsKey("拼音姓") && map.containsKey("拼音名")) || (map.containsKey("英文姓") && map.containsKey("英文名"))
					|| map.containsKey("英文姓名(姓/名)") || map.containsKey("英文姓名(姓 名)") || map.containsKey("英文姓名") || map.containsKey("漢語拼音姓名NAMEINHANYUPINYIN(SURNAME,OTHERNAME)")) {
				if (map.containsKey("姓 拼音") && map.containsKey("名 拼音")) {
					guFirstName = map.get("名 拼音").toString();
					guFamilyName = map.get("姓 拼音").toString();
				}else if (map.containsKey("拼音姓") && map.containsKey("拼音名")) {
					guFirstName = map.get("拼音名").toString();
					guFamilyName = map.get("拼音姓").toString();
				}else if (map.containsKey("英文姓") && map.containsKey("英文名")) {
					guFirstName = map.get("英文名").toString();
					guFamilyName = map.get("英文姓").toString();
				}else if (map.containsKey("英文姓名(姓/名)")) {
					String engName = map.get("英文姓名(姓/名)").toString();
					if (engName.split("/").length==2) {
						guFamilyName = engName.split("/")[0];
						guFirstName = engName.split("/")[1];
					}
				}else if (map.containsKey("英文姓名(姓 名)")) {
					String engName = map.get("英文姓名(姓 名)").toString();
					if (engName.split(" ").length==2) {
						guFamilyName = engName.split(" ")[0];
						guFirstName = engName.split(" ")[1];
					}
				}else if (map.containsKey("漢語拼音姓名NAMEINHANYUPINYIN(SURNAME,OTHERNAME)")) {
					String engName = map.get("漢語拼音姓名NAMEINHANYUPINYIN(SURNAME,OTHERNAME)").toString();
					if (engName.split("/").length==2) {
						guFamilyName = engName.split("/")[0];
						guFirstName = engName.split("/")[1];
					}
				}else if (map.containsKey("英文姓名")) {
					String engName = map.get("英文姓名").toString();
					if (engName.split("/").length==2) {
						guFamilyName = engName.split("/")[0];
						guFirstName = engName.split("/")[1];
					}
				}
			}
			passportInfo.setGuFirstNameEn(guFirstName);
			passportInfo.setGuFamiNameEn(guFamilyName);
			//性别
			String guSexCn = "";
			String guSexEn = "";
			if (map.containsKey("性别") || map.containsKey("性別SEX")) {
				Object sexObject = null;
				if (map.containsKey("性别")) {
					sexObject = map.get("性别");
				}else if (map.containsKey("性別SEX")) {
					sexObject = map.get("性別SEX");
				}
				if (Objects.nonNull(sexObject)) {
					String sex = sexObject.toString();
					if (sex.length() == 1) {
						boolean matches = sex.matches("[a-zA-Z]");
						if (matches) {
							if (Objects.equals(sex, "F")) {
								guSexCn = "女";
								guSexEn = "FEMALE";
							}else if (Objects.equals(sex, "M")) {
								guSexCn = "男";
								guSexEn = "MALE";
							}
						}else {
							if (Objects.equals(sex, "女")) {
								guSexCn = "女";
								guSexEn = "FEMALE";
							}else if (Objects.equals(sex, "男")) {
								guSexCn = "男";
								guSexEn = "MALE";
							}
						}
					}else if (sex.length() > 1) {
						if (Objects.equals(sex, "FEMALE")) {
							guSexCn = "女";
							guSexEn = "FEMALE";
						}else if (Objects.equals(sex, "MALE")) {
							guSexCn = "男";
							guSexEn = "MALE";
						}
					}
				}
			}
			passportInfo.setGuSexCn(guSexCn);
			passportInfo.setGuSexEn(guSexEn);
			//出生日期
			String birthDate = "";
			if (map.containsKey("出生日期") || map.containsKey("出生日期D.O.B(YYYY/MM/DD)")) {
				Object birthDateObject = null;
				if (map.containsKey("出生日期")) {
					birthDateObject = map.get("出生日期");
				}else if (map.containsKey("出生日期D.O.B(YYYY/MM/DD)")) {
					birthDateObject = map.get("出生日期D.O.B(YYYY/MM/DD)");
				}
				System.out.println("-----------"+birthDateObject);
				if (Objects.nonNull(birthDateObject)) {
					Date birthDateDate = DateUtil.convertStringToDate(birthDateObject);
					if (Objects.nonNull(birthDateDate)) {
						System.out.println("-----===-----"+birthDateDate);
						birthDate = new SimpleDateFormat("yyyyMMdd").format(birthDateDate);
					}
				}
			}
			passportInfo.setGuBirthDate(birthDate);
			//出生地
			String guBirthPlaceCn = "";
			String guBirthPlaceEn = "";
			if (map.containsKey("出生地") || map.containsKey("出生地PLACEOFBIRTH")) {
				Object birthPlaceObject = null;
				if (map.containsKey("出生地")) {
					birthPlaceObject = map.get("出生地");
				}else if (map.containsKey("出生地PLACEOFBIRTH")) {
					birthPlaceObject = map.get("出生地PLACEOFBIRTH");
				}
				if (Objects.nonNull(birthPlaceObject)) {
					String birthPlace = birthPlaceObject.toString().replaceAll("\\s*", "");
					//判断是字符还是汉字
					if (birthPlace.matches("[a-zA-z]+")) {
						guBirthPlaceCn =  StringUtil.getCityInfo(birthPlace);
						guBirthPlaceEn = birthPlace;
					}else {
						guBirthPlaceCn = birthPlace;
						guBirthPlaceEn = PinyinUtil.getPinyin(birthPlace).toUpperCase();
					}
				}
			}
			passportInfo.setGuBirthPlaceCn(guBirthPlaceCn);
			passportInfo.setGuBirthPlaceEn(guBirthPlaceEn);
			//护照号
			String guPassportNum = "";
			if (map.containsKey("护照号") || map.containsKey("护照号码") ||map.containsKey("证件号码") || map.containsKey("證件號碼") || map.containsKey("證件號碼T/DNO.")) {
				if (map.containsKey("护照号")) {
					guPassportNum = map.get("护照号").toString();
				}else if (map.containsKey("护照号码")) {
					guPassportNum = map.get("护照号码").toString();
				}else if (map.containsKey("证件号码")) {
					guPassportNum = map.get("证件号码").toString();
				}else if (map.containsKey("證件號碼T/DNO.")) {
					guPassportNum = map.get("證件號碼T/DNO.").toString();
				}
			}
			passportInfo.setGuPassportNum(guPassportNum);
			//护照签发日期
			String guIssueDate = "";
			if (map.containsKey("签发日期") || map.containsKey("护照签发日期") || map.containsKey("签发日期DATEOFISSUE")) {
				Object issueDateObject = null;
				if (map.containsKey("签发日期")) {
					issueDateObject = map.get("签发日期");
				}else if (map.containsKey("护照签发日期")) {
					issueDateObject = map.get("护照签发日期");
				}else if (map.containsKey("签发日期DATEOFISSUE")) {
					issueDateObject = map.get("签发日期DATEOFISSUE");
				}
				if (Objects.nonNull(issueDateObject)) {
					Date issueDateDate = DateUtil.convertStringToDate(issueDateObject);
					if (Objects.nonNull(issueDateDate)) {
						guIssueDate = new SimpleDateFormat("yyyyMMdd").format(issueDateDate);
					}
				}
			}
			passportInfo.setGuIssueDate(guIssueDate);
			//签发地
			String guIssuePlaceCn = "";
			String guIssuePlaceEn = "";
			if (map.containsKey("签发地") || map.containsKey("护照签发地") || map.containsKey("簽發地PLACEOFISSUE")) {
				Object placeObject = null;
				if (map.containsKey("签发地")) {
					placeObject = map.get("签发地");
				}else if (map.containsKey("护照签发地")) {
					placeObject = map.get("护照签发地");
				}else if (map.containsKey("簽發地PLACEOFISSUE")) {
					placeObject = map.get("簽發地PLACEOFISSUE");
				}
				if (Objects.nonNull(placeObject)) {
					String issuePlace = placeObject.toString().replaceAll("\\s*", "");
					guIssuePlaceCn = issuePlace;
					guIssuePlaceEn = PinyinUtil.getPinyin(issuePlace).toUpperCase();
				}
			}
			passportInfo.setGuIssuePlaceCn(guIssuePlaceCn);
			passportInfo.setGuIssuePlaceEn(guIssuePlaceEn);
			//护照有效期
			String guExpiryDate = "";
			if (map.containsKey("有效期") || map.containsKey("有效日期") || map.containsKey("护照有效期") || map.containsKey("护照有效日期") || map.containsKey("有效期DATEOFEXPIRY")) {
				Object expiryDateObject = null;
				if (map.containsKey("有效期")) {
					expiryDateObject = map.get("有效期");
				}else if (map.containsKey("有效日期")) {
					expiryDateObject = map.get("有效日期");
				}else if (map.containsKey("护照有效期")) {
					expiryDateObject = map.get("护照有效期");
				}else if (map.containsKey("护照有效日期")) {
					expiryDateObject = map.get("护照有效日期");
				}else if (map.containsKey("有效期DATEOFEXPIRY")) {
					expiryDateObject = map.get("有效期DATEOFEXPIRY");
				}
				if (Objects.nonNull(expiryDateObject)) {
					Date expiryDateDate = DateUtil.convertStringToDate(expiryDateObject);
					if (Objects.nonNull(expiryDateDate)) {
						guExpiryDate = new SimpleDateFormat("yyyyMMdd").format(expiryDateDate);
					}
				}
			}
			passportInfo.setGuExpiryDate(guExpiryDate);
			
			//国籍
			String guNationalityCn = "";
			String guNationalityEn = "";
			if (map.containsKey("国籍")) {
				Object nation = map.get("国籍");
				System.out.println(nation);
				if (Objects.nonNull(nation)) {
					String guNation = nation.toString().replaceAll("\\s*", "");
					String nationaInfo = StringUtil.getNationaInfo(guNation);
					System.out.println(nationaInfo);
					if (nationaInfo.contains("/")) {
						guNationalityCn = nationaInfo.split("/")[0];
						guNationalityEn = nationaInfo.split("/")[1].toUpperCase();
					}else {
						throw new Exception("国籍识别有误");
					}
				}
			}
			passportInfo.setGuNationalityCn(guNationalityCn);
			passportInfo.setGuNationalityEn(guNationalityEn);
			if (Objects.isNull(passportInfo.getGuPassportNum())) {
				throw new Exception("缺少护照号码");
			}
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e.getMessage());
		}
		return passportInfo;
	}
    
    @SuppressWarnings("unused")
	public static void main(String[] args) {
        String filePath = "C:\\Users\\Administrator\\Desktop\\111.xls";
        
        InputStream fileInputStream;
		try {
			fileInputStream = new FileInputStream(new File(filePath));
			ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader(fileInputStream);
			//获取列明所在行号
			int lineNoRow = 99;
			int lineNameRow = 99;
			List<List<Object>> readallData = reader.read();
			for (int i = 0; i < readallData.size(); i++) {
				String rowString = readallData.get(i).toString();
				if ((rowString.contains("證件") || rowString.contains("护照") || rowString.contains("证件")) && (rowString.contains("性别") || rowString.contains("性別")) && rowString.contains("出生")) {
					if (lineNameRow==99) {
						lineNameRow = i;
					}
					if (lineNoRow!=99 && lineNameRow!=99) {
						break;
					}
				}
				List<Object> nolist = readallData.get(i);
				for (int j = 0; j < nolist.size(); j++) {
					if (Objects.nonNull(nolist.get(j))) {
						String noLieString = nolist.get(j).toString();
						if (noLieString.contains("序号") || noLieString.contains("编号") ||  noLieString.contains("編號")) {
							if (lineNoRow==99) {
								lineNoRow = j;
							}
							if (lineNoRow!=99 && lineNameRow!=99) {
								break;
							}
						}
					}
				}
			}
//			logger.error("lineNoRow:"+lineNoRow+",lineNameRow:"+lineNameRow);
			//获取数据
	        if (lineNameRow==99 || lineNoRow==99) {
				throw new Exception("未获取到列名、序号所在列");
			}else {
    			//获取列明行数据
    			List<Object> lineNameList = readallData.get(lineNameRow);
				List<Map<String,Object>> linkedList = new LinkedList<>();
				System.out.println(lineNameList);
				//数据在列名所咋行的下一行
				for (int i = lineNameRow+1; i < readallData.size(); i++) {
					List<Object> rowlist = readallData.get(i);
					//通过第一列的数字来判断是否到达最后一行
					if (!rowlist.get(lineNoRow).toString().contains("T") && !StringUtils.isNumeric(rowlist.get(lineNoRow).toString())) {
						break;
					}
					Map<String,Object> hashMap = new HashMap<String,Object>();
					//获取列明和数字组成map
					for (int j = 0; j < lineNameList.size(); j++) {
//						System.out.println(lineNameList.get(j)+":"+rowlist.get(j));
						Object cellobject =  rowlist.get(j);
						//获取key
						String mapKey = lineNameList.get(j).toString().replaceAll("\n", "").replaceAll("\\s*", "");
						if (cellobject==null || Objects.equals(cellobject.toString(), "")) {
							continue;
						}
						hashMap.put(mapKey, cellobject);
					}
					linkedList.add(hashMap);
				}
				
				List<PassportInfo> linkedList2 = new LinkedList<>();
				for (int i = 0; i < linkedList.size(); i++) {
					
					Map<String, Object> map = linkedList.get(i);
					PassportInfo mapconverPassportInfo = mapconverPassportInfo(map);
					linkedList2.add(mapconverPassportInfo);
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    
    /**
     * BufferedInputStream存储文件
     * @param filePath
     * @param bodyStream
     */
    public static String saveFilebyRead(String dirPath, String fileName,BufferedInputStream bodyStream) {
		
    	String urlStr = "";
    	String filePath = dirPath + "/" + fileName;
		try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
            byte[] buffer = new byte[1024]; // 缓冲区大小，可以根据需要调整
            int length;
            
            // 读取并写入数据
            while ((length = bodyStream.read(buffer)) != -1) {
                bos.write(buffer, 0, length);
            }
            // 刷新输出流以确保所有数据都写入目标文件
            bos.flush();
            
            InputStream fileInputStream = new FileInputStream(new File(filePath));
			String pictrueToBase64 = QiniuUpLoad.pictrueToBase64(fileInputStream);
			try {
				urlStr = "https://cdn.travbao.com/"+new QiniuUpLoad().pictrueUpLoadQiniu(pictrueToBase64, fileName);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            System.out.println("文件复制成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
		return urlStr;
    }
}
